package com.badshun.box2d;

import com.badlogic.gdx.physics.box2d.*;
import com.badshun.player.*;
import com.badshun.stage.*;
import com.badshun.sound.*;
import com.badlogic.gdx.audio.*;

public class Box2DContactListener implements ContactListener {
	private MainPlayer main;
	private NPCPlayers npcs;
	private EnvironmentStageManager esm;
	private GameBackgroundMusic gbm;
	private Music bg1;
	
	private int index;
	
	public Box2DContactListener(MainPlayer main,NPCPlayers npcs,EnvironmentStageManager esm) {
		this.main = main;
		this.npcs = npcs;
		this.esm = esm;
		
		gbm = new GameBackgroundMusic();
	}
	
	@Override
	public void beginContact(Contact p1) {
		Fixture a = p1.getFixtureA();
		Fixture b = p1.getFixtureB();
		
		for(int i = 0; i < npcs.list.getLength(); i++) {
			if(a != null && a.getUserData() != null && b != null && b.getUserData() != null){
				if((a.getUserData().equals("PLAYER") && b.getUserData().equals(npcs.list.getChild(i).getName())) || a.getUserData().equals(npcs.list.getChild(i).getName()) && b.getUserData().equals("PLAYER")) {
					index = i;
					
					main.isHitNPC = true;
					npcs.list.getChild(i).isHitPlayer = true;
				}
			}
		}
		
		if(a != null && a.getUserData() != null && b != null && b.getUserData() != null){
			if((a.getUserData().equals("PLAYER") && b.getUserData().equals("DOOR"))) {
				main.isHitDoor = true;
				
				main.hiter =  b.getBody().getUserData().toString().replace("DOOR","");
			} else if(a.getUserData().equals("DOOR") && b.getUserData().equals("PLAYER")) {
				main.isHitDoor = true;
				
				main.hiter =  a.getBody().getUserData().toString().replace("DOOR","");
			}
		}
		
		for(int i = 0; i < esm.mcs.enemys.length; i ++) {
			if(a != null && a.getUserData() != null && b != null && b.getUserData() != null){
				if((a.getUserData().equals("PLAYER") && b.getUserData().toString().endsWith("CIRCLR_ENEMY"))) {
					main.isHitEnemy = true;

					if(esm.stage instanceof MainCityStage && b.getUserData().toString().startsWith(esm.mcs.enemys[i].name)) {
						main.enemyName = esm.mcs.enemys[i].name;
					}
				} else if(a.getUserData().toString().endsWith("CIRCLR_ENEMY") && b.getUserData().equals("PLAYER")) {
					main.isHitEnemy = true;

					if(esm.stage instanceof MainCityStage && b.getUserData().toString().startsWith(esm.mcs.enemys[i].name)) {
						main.enemyName = esm.mcs.enemys[i].name;
					}
				}
				
				if((a.getUserData().toString().endsWith("SKILL") && b.getUserData().toString().endsWith("ENEMY"))) {
					esm.mcs.enemys[i].isHitSkill = true;
					bg1 = gbm.createMusic("bgm/a.m4a");
					bg1.play();
					bg1.setLooping(false);
					bg1.setVolume(1);
				} else if(a.getUserData().toString().endsWith("ENEMY") && b.getUserData().toString().endsWith("SKILL")) {
					esm.mcs.enemys[i].isHitSkill = true;
					bg1 = gbm.createMusic("bgm/a.m4a");
					bg1.play();
					bg1.setLooping(false);
					bg1.setVolume(1);
				}
			}
		}
	}

	@Override
	public void endContact(Contact p1) {
		main.isHitNPC = false;
		npcs.list.getChild(index).isHitPlayer = false;
		
		main.isHitDoor = false;
		main.enemyName = "";
		
		System.out.println("stop");
		
		for(int i = 0; i < esm.mcs.enemys.length; i++) {
			if(esm.mcs.enemys[i].isHitSkill) {
				esm.mcs.enemys[i].HP--;
				System.out.println(esm.mcs.enemys[i].HP);
			}
		}
	}

	@Override
	public void preSolve(Contact p1, Manifold p2) {}

	@Override
	public void postSolve(Contact p1, ContactImpulse p2) {}
}
